package controller.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.ResourceType;
import model.Role;

import db.DAOFactory;

/**
 * 
 * Description: 对管理员进行资源的权限级别的设置请求进行处理。
 * 
 * @since  2009/3/4
 * @author ashen
 * 
 */
public class SetPrivilege extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SetPrivilege() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String uri=request.getRequestURI();
		System.out.println(uri);
		String[] uriStr=uri.split("/");
        int RoleId=Integer.parseInt(uriStr[uriStr.length-1]);
		//输出资源权限设置列表
		tools.OutputXml.output(new model.dto.Privileges(RoleId), response);
				
	}

	/**
	 * The doPut method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to put.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("receive put request.");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//接受来自客户端请求中的数据
		Scanner s = new Scanner(request.getInputStream());
		String data=null;
		if(s.hasNextLine())
			data=s.nextLine();
		System.out.println(data);
		String[] paras=data.split(",");
		model.ResourceType type = new model.ResourceType();
		//将从客户端得到的资源类型赋值给资源类型对象type的type属性
		type.setType(paras[1]);
		model.relation.Operation operation=new model.relation.Operation();
		operation.setDelete(Integer.parseInt(paras[4]));
		operation.setGet(Integer.parseInt(paras[2]));
		operation.setPost(Integer.parseInt(paras[5]));
		operation.setPut(Integer.parseInt(paras[3]));
		operation.setType(type);
		operation.setRoleId(Integer.parseInt(paras[0]));
		if(DAOFactory.getOperationDAO().update(operation))
			out.println("修改成功.");
		else
			out.println("修改失败!");
		out.flush();
		out.close();
					
	}
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
